<template>
  <div>
    <el-dialog title="上报事件" :visible.sync="dialogShow" width="70%" :close-on-click-modal="false" :close-on-press-escape="false" :append-to-body="true">
      <form-common :filed-list="projectEventAddList" ref="form" @selectChange="selectChange"></form-common>
      <file-view ref="fileView"></file-view>

      <div slot="footer" class="dialog-footer margin-bottom-40" v-if="btnShow">
        <el-button size="mini" class="f-right el-button-reset" type="primary" @click="saveFn('publish')">发 布</el-button>
        <el-button size="mini" class="f-right margin-right-15" type="primary" @click="saveFn">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import formCommon from '@g/components/form/formCommon.vue';
  import { projectEventAddList } from '../form';
  import { apiWorkAdd, apiWorkUpdate, apiOrgList, apiManagerList } from '../../../api/workTrace';
  import { projectList } from '@/api';
  import { apiConstant } from '../../../api/common';
  import { requestTips } from '../../../assets/util';
  import fileView from './fileView.vue';

  export default {
    name: 'addWork',
    props: {
      btnShow: {
        type: Boolean,
        default: true,
      },
      isProjectKey: {
        type: Boolean,
        default: false,
      },
      projectKey: {
        type: String,
        default: '',
      },
    },
    data() {
      return {
        dialogShow: false,
        projectEventAddList: projectEventAddList,
        workObj: {},
        icon: '',
        isInfo: false,
      };
    },
    created() {
      if (this.isProjectKey) {
        this.projectEventAddList.shift();
      }
    },
    mounted() {
      this.getConstant();
      this.getOrgList();
      this.getProjectList();
    },
    methods: {
      init() {
        this.workObj = {};
        this.dialogShow = true;
        this.initFormReadOnly(projectEventAddList, false);
        this.$nextTick(() => {
          this.$refs.fileView.backInfo([], []);
          this.$refs.form.clearValidateForm();
          if (this.projectKey) {
            this.initProjectKey();
          }
        });
      },

      //常量
      async getConstant() {
        let res = await apiConstant();
        projectEventAddList.forEach(item => {
          if (item.bindFieldName == 'workType') {
            res.workType.pop();
            res.workType.pop();
            res.workType.pop();
            item.optionList = res.workType;
          }
          if (item.bindFieldName == 'importantDegree') {
            item.optionList = res.workImportantDegree;
          }
        });
      },

      //项目列表
      async getProjectList() {
        const res = await projectList({});
        projectEventAddList.forEach(item => {
          if (item.bindFieldName == 'projectKey') {
            item.optionList = res.rows || [];
          }
        });
      },

      //部门数据源
      async getOrgList() {
        let res = await apiOrgList();
        projectEventAddList.forEach(item => {
          if (item.bindFieldName == 'organizationKey') {
            item.optionList = res.rows || [];
          }
        });
      },

      //工作负责人数据源
      async getManagerList(key) {
        let res = await apiManagerList({
          orgKeys: [key],
        });
        projectEventAddList.forEach(item => {
          if (item.bindFieldName == 'processorKey') {
            item.optionList = res.rows || [];
          }
        });
      },

      //保存
      async saveFn(type) {
        let model = this.$refs.form.validateForm();
        if (!model) return;
        model.imageAttachmentList = this.$refs.fileView.getPhotoList();
        model.fileAttachmentList = this.$refs.fileView.getFileList();
        model.status = type == 'publish' ? 1 : 2;
        model.labelType = 2;
        if (this.workObj.primaryKey) {
          model.primaryKey = this.workObj.primaryKey;
        }
        //会议纪要时添加工作
        if (this.$route.query.meetingKey) {
          model.bizId = this.$route.query.meetingKey;
          model.bizType = 'meeting';
        }
        let res = this.workObj.primaryKey ? await apiWorkUpdate(model) : await apiWorkAdd(model);
        requestTips(res, async () => {
          this.dialogShow = false;
          this.$emit('refresh', this.workObj.primaryKey);
        });
      },

      //表单事件
      selectChange(data) {
        if (data.filedName == 'startTime' || data.filedName == 'endTime') {
          let timestamp = data.model.endTime - data.model.startTime;
          if (timestamp < 0) {
            if (data.filedName == 'endTime') {
              data.cb('endTime', '');
              this.$message.warning('截止日期应大于开始日期');
            }
            if (data.filedName == 'startTime' && data.model.endTime) {
              data.cb('endTime', '');
              this.$message.warning('截止日期应大于开始日期');
            }
          }
        }
        if (data.filedName == 'organizationKey') {
          this.getManagerList(data.filedVal);
          data.cb('processorKey', '');
        }
      },

      //编辑回显
      callBackFn(obj) {
        this.dialogShow = true;
        obj.imageAttachmentList &&
          obj.imageAttachmentList.forEach(item => {
            item.url = item.fileUrl;
            item.key = item.fileId;
            item.name = item.fileTitle;
          });
        obj.fileAttachmentList &&
          obj.fileAttachmentList.forEach(item => {
            item.url = item.fileUrl;
            item.key = item.fileId;
            item.name = item.fileTitle;
            item.originalName = item.fileTitle;
          });
        this.workObj = obj;
        this.$nextTick(() => {
          this.$refs.form.initModel();
          projectEventAddList[0].bindFieldVal = '';
          this.initFormReadOnly(projectEventAddList, !this.btnShow);
          this.projectEventAddList = [];
          this.setFiledVal(projectEventAddList, obj);
          this.projectEventAddList = projectEventAddList;
          this.getManagerList(obj.organizationKey);
          this.$refs.fileView.backInfo(obj.imageAttachmentList || [], obj.fileAttachmentList || []);
        });
      },

      //回显
      setFiledVal(list, row) {
        list.forEach(item => {
          for (let i in row) {
            if (i == item.bindFieldName) {
              item.bindFieldVal = row[i];
            }
          }
        });
      },

      //初始化filed只读
      initFormReadOnly(list, type) {
        list.forEach(item => {
          if (item.bindFieldName != 'createUserName') {
            item.isReadOnly = type;
            item.isRequired = !type;
          }

          if (item.type == 'select' || item.type == 'date' || item.type == 'radio' || item.type == 'checkbox') {
            if (item.bindFieldName != 'createUserName') {
              item.isDisabled = type;
            }
          }
        });
      },

      initProjectKey() {
        projectEventAddList.forEach(item => {
          if (item.bindFieldName == 'projectKey') {
            item.bindFieldVal = this.projectKey;
            item.isDisabled = true;
          }
        });
      },
    },
    components: { formCommon, fileView },
  };
</script>

<style lang="scss" scoped>
  .upload-box {
    margin-left: 44px;
  }
  ::v-deep .el-form .el-form-item__label {
    padding-left: 0;
  }
  .required {
    & > .el-form-item {
      & > .el-form-item__label:before {
        content: '*';
        color: #f56c6c;
        margin-right: 4px;
      }
    }
  }
</style>
